package com.yinp.proapp.module.study.jiguangloginshare.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.yinp.proapp.utils.AppUtils;

/**
 * @author :yinpeng
 * date      :2021/8/26
 * package   :com.yinp.proapp.module.study.jiguangloginshare.view
 * describe  :
 */
public class TwoSelectView extends View {

    public TwoSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TwoSelectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    Paint paint;
    Paint paintTextOne;
    Paint paintTextTwo;
    private float textHeight;
    private float radius;
    private float addWidth = 10;

    private void init(Context context, AttributeSet attrs) {
        setBackground(null);
        paint = new Paint();
        paintTextOne = new Paint();
        paintTextTwo = new Paint();
        paintTextOne.setColor(Color.WHITE);
        paintTextTwo.setColor(Color.WHITE);
        paintTextOne.setTextSize(AppUtils.dpToPx(20));
        paintTextTwo.setTextSize(AppUtils.dpToPx(20));
        float textOneWidth = paintTextOne.measureText("极光登录");

        Paint.FontMetrics fontMetrics = paintTextOne.getFontMetrics();
        textHeight = fontMetrics.descent - fontMetrics.ascent;
        radius = textOneWidth - textHeight > 0 ? textOneWidth / 2 : textHeight / 2;
        radius += addWidth;
        Log.d("abcd", "init: " + radius);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension((int) (radius * 3), (int) (radius * 4));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (event.getY() < radius * 2) {
                    if (event.getX() > radius) {
                        if (cLickListener != null) {
                            cLickListener.click(false);
                        }
                    }
                } else {
                    if (event.getX() < radius * 2) {
                        if (cLickListener != null) {
                            cLickListener.click(true);
                        }
                    }
                }
                break;
        }
        return false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.RED);
        canvas.drawCircle(radius, radius * 3, radius, paint);
        canvas.drawText("极光登录", addWidth, radius * 3 + textHeight / 4, paintTextOne);
        canvas.drawCircle(radius * 2, radius, radius, paint);
        canvas.drawText("分享", addWidth + radius, radius + textHeight / 4, paintTextTwo);
    }

    public abstract static class CLickListener {
        public void click(boolean isLeft) {
        }
    }

    public CLickListener cLickListener;

    public void setcLickListener(CLickListener cLickListener) {
        this.cLickListener = cLickListener;
    }
}
